"""
xapper unit tests.
"""

from __future__ import unicode_literals

import os, sys
from mox import *

import xapper

class UnitTests(MoxTestBase):

    def test_filesystem_finder(self):
      n = lambda p: os.path.normcase(os.path.abspath(p))
      self.mox.StubOutWithMock(os.path, "exists")
      os.path.exists(n("/a/b/c/foo.bar.xap")).AndReturn(False)
      os.path.exists(n("/d/e/f/foo.bar.xap")).AndReturn(False)
      os.path.exists(n("/a/b/c/foo.baz.xap")).AndReturn(True)
      os.path.exists(n("/d/e/f/foo.baz.xap")).AndReturn(False)
      os.path.exists(n("/a/b/c/foo.bat.xap")).AndReturn(False)
      os.path.exists(n("/d/e/f/foo.bat.xap")).AndReturn(True)
      os.path.exists(n("/a/b/c/foo.bam.xap")).AndReturn(True)
      os.path.exists(n("/d/e/f/foo.bam.xap")).AndReturn(True)
      self.mox.ReplayAll()
      fsf = xapper.finders.FilesystemFinder([n("/a/b/c"), n("/d/e/f")])
      result = list(fsf.find("foo.bar.xap"))
      self.assertEqual(result, [])
      result = list(fsf.find("foo.baz.xap"))
      self.assertEqual(result, [n("/a/b/c/foo.baz.xap")])
      result = list(fsf.find("foo.bat.xap"))
      self.assertEqual(result, [n("/d/e/f/foo.bat.xap")])
      result = list(fsf.find("foo.bam.xap"))
      self.assertEqual(result, [n("/a/b/c/foo.bam.xap"), n("/d/e/f/foo.bam.xap")])
      result = list(fsf.find(os.path.join("..", "foo.bab.xap")))
      self.assertEqual(result, [])

    def test_app_directory_finder(self):
      n = lambda p: os.path.normcase(os.path.abspath(p))
      self.mox.StubOutWithMock(xapper.finders, "settings")
      xapper.finders.settings.INSTALLED_APPS = ["xapper"]
      self.mox.StubOutWithMock(os.path, "exists")
      self.mox.StubOutWithMock(os.path, "isdir")
      xapper_dir = n(os.path.dirname(xapper.__file__))
      os.path.isdir(os.path.join(xapper_dir, "foo")).AndReturn(True)
      os.path.exists(os.path.join(xapper_dir, "foo", "bar")).AndReturn(False)
      os.path.exists(os.path.join(xapper_dir, "foo", "baz")).AndReturn(True)
      self.mox.ReplayAll()
      adf = xapper.finders.AppDirectoryFinder("foo")
      result = list(adf.find("bar"))
      self.assertEqual(result, [])
      result = list(adf.find("baz"))
      self.assertEqual(result, [os.path.join(xapper_dir, "foo", "baz")])

    def test_finder(self):
      # Mock the settings
      self.mox.StubOutWithMock(xapper.finder, "settings")
      xapper.finder.settings.XAPPER_TEST_FINDERS = (
        "xapper.finders.test_finder",
      )
      # Mock the finder
      mock_finder = self.mox.CreateMock(xapper.finders.FilesystemFinder)
      mock_finder.find("foo").AndReturn((
        "/filtered/out",
        "/not/filtered/out",
      ))
      # Mock __import__
      builtin = sys.modules["__builtin__"]
      self.mox.StubOutWithMock(builtin, "__import__")
      mock_module = self.mox.CreateMockAnything()
      mock_module.test_finder = mock_finder
      __import__("xapper.finders", IsA(dict), IsA(dict), ["test_finder"]).AndReturn(mock_module)
      # Mock the filter
      mock_filter = self.mox.CreateMockAnything()
      mock_filter("/filtered/out").AndReturn(False)
      mock_filter("/not/filtered/out").AndReturn(True)
      # Try it out
      self.mox.ReplayAll()
      find = xapper.finder.finder_func("XAPPER_TEST_FINDERS", mock_filter)
      result = list(find("foo"))
      self.assertEqual(result, ["/not/filtered/out"])
